
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

class MyButtonContainer extends MyComponentContainer {

    private JTextField buttonText;
    private JButton jButton;
    public byte operation;

    //RecordNavigation, RecordOperations, Form Operations
    public static final byte RecordNavigation = 01;
    public static final byte RecordOperations = 02;
    public static final byte FormOperations = 03;
    public static final byte FindNext = 1;
    public static final byte FindRecord = 2;
    public static final byte GoToFirstRecord = 3;
    public static final byte GoToLastRecord = 4;
    public static final byte GoToNextRecord = 5;
    public static final byte GoToPreviuosRecord = 6;
    public static final byte AddNewRecord = 7;
    public static final byte DeleteRecord = 8;
    public static final byte DuplicateRecord = 9;
    public static final byte PrintRecord = 10;
    public static final byte SaveRecord = 11;
    public static final byte UndoRecord = 12;
    //"ApplyFromFilter","CloseForm","OpenForm","PrintForm","Refresh Form Data";
    public static final byte ApplyFromFilter = 13;
    public static final byte CloseForm = 14;
    public static final byte OpenForm = 15;
    public static final byte PrintForm = 16;
    public static final byte RefreshFormData = 17;

    MyButtonContainer(String name, MyFrame frame) {
        super(new JButton(name), frame);
        jButton = (JButton) getComponent();
        jButton.setRolloverEnabled(false);
        jButton.addMouseListener(new MouseHandler());
        jButton.addMouseMotionListener(new MouseDragHandler());
    }

    MyButtonContainer(Icon icon, MyFrame frame) {
        super(new JButton(icon), frame);
        jButton = (JButton) getComponent();
        jButton.setRolloverEnabled(false);
        jButton.addMouseListener(new MouseHandler());
        jButton.addMouseMotionListener(new MouseDragHandler());
    }
    //#########################################################################

    public byte getOperation() {
        return operation;
    }
    
    public JButton getJButton() {
        return jButton;
    }
    //#########################################################################

    public void setOperation(byte operation) {
        this.operation = operation;
    }
    //#########################################################################

    public class MouseHandler extends MouseAdapter {

        @Override
        public void mouseEntered(MouseEvent m) {
            if (isChosen() == true) {
                if (jButton.getIcon() == null) {
                    frame.panel.setCursor(new Cursor(Cursor.TEXT_CURSOR));
                } else {
                    frame.panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
            }
        }

        @Override
        public void mouseExited(MouseEvent m) {
            frame.panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }

        @Override
        public void mouseClicked(MouseEvent m) {
            if (m.getButton() == 3) {
                setChosen(true);
                showBorder();
                handleRightClickSelection();
                MyRightClickMenu popUpMenu = new MyRightClickMenu(MyButtonContainer.this, m.getX(), m.getY(), frame);
            }
            if (m.getButton() == MouseEvent.BUTTON1) {
                if (m.getClickCount() == 2) {
                    //System.out.println("\n---------------->it is double yaaaaaaaaa\n");
                    JComponent eventObject = (JComponent) m.getComponent(); /* Get the component that is
                    responsable for the action */
                    MyPropertiesWindow properties = new MyPropertiesWindow(MyButtonContainer.this, frame);
                /*Createing the properties window and send the object that is
                resposable for the event*/
                } else if (m.getClickCount() == 1 & m.isShiftDown() == false) {
                    if (isChosen() == false) {
                        if (jButton.getIcon() == null) {
                            frame.panel.setCursor(new Cursor(Cursor.TEXT_CURSOR));
                        } else {
                            frame.panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                        }
                        setChosen(true);
                        showBorder();
                    } else {
                        if (jButton.getIcon() == null) {
                            hideBorder();
                            jButton.setVisible(false);
                            if (buttonText == null) {
                                buttonText = new JTextField(jButton.getText());
                            }
                            buttonText.setLocation(jButton.getX(), jButton.getY());
                            buttonText.setSize(jButton.getWidth(), jButton.getHeight());
                            buttonText.setHorizontalAlignment(JTextField.CENTER);

                            buttonText.addFocusListener(new FocusAdapter() {

                                @Override
                                public void focusLost(FocusEvent f) {
                                    jButton.setText(buttonText.getText());
                                    frame.panel.remove(buttonText);
                                    jButton.setVisible(true);
                                    frame.panel.repaint();
                                    flagUndo = true;
                                    handleUndo();
                                }
                            });

                            buttonText.addKeyListener(new KeyAdapter() {

                                @Override
                                public void keyPressed(KeyEvent k) {
                                    if (KeyEvent.getKeyText(k.getKeyCode()).equals("Enter")) {
                                        jButton.setText(buttonText.getText());
                                        frame.panel.remove(buttonText);
                                        jButton.setVisible(true);
                                        showBorder();
                                        frame.panel.repaint();
                                    }
                                }
                            });

                            frame.panel.add(buttonText);
                            buttonText.grabFocus();
                        }
                    }
                }
            }
        }

        @Override
        public void mousePressed(MouseEvent m) {
            frame.panel.requestFocusInWindow();

            if (m.getButton() == MouseEvent.BUTTON1) {
                handleSelection(m);
                frame.panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                clickToAnchorX = m.getX();
                clickToAnchorY = m.getY();
            }
        }

        @Override
        public void mouseReleased(MouseEvent m) {
            if (m.getButton() == MouseEvent.BUTTON1) {
                frame.panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
            handleUndo();
        }
    }
    //#########################################################################

    class MouseDragHandler extends MouseMotionAdapter {

        @Override
        public void mouseDragged(MouseEvent evt) {
            handleDrag(evt);
        }
    }
    //#########################################################################
}